<template>
	<view class="pages">
		<!-- 头部 -->
		<view class="header_bar">
			<view class="topBg">
				<image src="../../static/pack/indexLogo.png" mode=""></image>
				<view class="nameBox">
					<view class="name">Hello！</view>
					<view class="name">欢迎登录嗖嗖商城</view>
				</view>
			</view>
		</view>
		
		<view class="">
			<view class="operateBox-tabs operateBox-tabs--top">
				<view class="operateBox-formBox">
					<view class="operateBox-formBox-list-header">昵称</view>
					<view class="operateBox-formBox-list operateBox-formBox-listBorder" :class="{ 'operateBox-formBox-list-input--active': isFocus == 0, showCloseBtn: name != '' }">
						<input type="text" placeholder="请输入用户昵称" v-model="name" placeholder-class="inputStyle"
						@blur="blurInput(0)"
						@focus="focusInput(0)"/>
						<view class="operateBox-formBox-list-image">
							<view class="operateBox-formBox-list-image__clear operateBox-formBox-list-image__clear--active" @click="clearData('name')">
								<image src="../../static/public/icon-clear.png" mode="widthFix"></image>
							</view>
						</view>
					</view>
				</view>
				<view class="operateBox-formBox">
					<view class="operateBox-formBox-list-header">手机号</view>
					<view class="operateBox-formBox-list operateBox-formBox-listBorder" :class="{ 'operateBox-formBox-list-input--active': isFocus == 1, showCloseBtn: phone != '' }">
						<input type="number" placeholder="请输入手机号" v-model="phone" placeholder-class="inputStyle"
						@blur="blurInput(1)"
						@focus="focusInput(1)"/>
						<view class="operateBox-formBox-list-image">
							<view class="operateBox-formBox-list-image__clear operateBox-formBox-list-image__clear--active" @click="clearData('phone')">
								<image src="../../static/public/icon-clear.png" mode="widthFix"></image>
							</view>
						</view>
					</view>
				</view>
				<view class="operateBox-formBox">
					<view class="operateBox-btn--between">
						<view class="operateBox-formBox-list-header">登录密码</view>
					</view>
					<view class="operateBox-formBox-list operateBox-formBox-listBorder" :class="{ 'operateBox-formBox-list-input--active': isFocus == 4, showCloseBtn: password != '' }">
						<input placeholder="请输入登录密码" placeholder-class="inputStyle" v-model="password"
						:password="!eyeShowHideArray[4].show"
						@blur="blurInput(4)"
						@focus="focusInput(4)"/>
						<view class="operateBox-formBox-list-image">
							<view class="operateBox-formBox-list-image__clear operateBox-formBox-list-image__clear--active" @click="clearData('password')">
								<image src="../../static/public/icon-clear.png" mode="widthFix"></image>
							</view>
							<view class="operateBox-formBox-list-image__see" @click="toggleEyeStatus(4)">
								<image :src="eyeShowHideArray[4].show ? eyeShow : eyeHide" mode="widthFix"></image>
							</view>
						</view>
					</view>
				</view>
				<view class="operateBox-formBox">
					<view class="operateBox-formBox-list-header">交易密码</view>
					<view class="operateBox-formBox-list operateBox-formBox-listBorder" :class="{ 'operateBox-formBox-list-input--active': isFocus == 5, showCloseBtn: pay_password != '' }">
						<input placeholder="请输入交易密码" placeholder-class="inputStyle" v-model="pay_password"
						:password="!eyeShowHideArray[5].show"
						@blur="blurInput(5)"
						@focus="focusInput(5)"/>
						<view class="operateBox-formBox-list-image">
							<view class="operateBox-formBox-list-image__clear operateBox-formBox-list-image__clear--active" @click="clearData('pay_password')">
								<image src="../../static/public/icon-clear.png" mode="widthFix"></image>
							</view>
							<view class="operateBox-formBox-list-image__see" @click="toggleEyeStatus(5)">
								<image :src="eyeShowHideArray[5].show ? eyeShow : eyeHide" mode="widthFix"></image>
							</view>
						</view>
					</view>
				</view>
				<view class="operateBox-formBox">
					<view class="operateBox-formBox-list-header">邀请码</view>
					<view class="operateBox-formBox-list operateBox-formBox-listBorder" :class="{ 'operateBox-formBox-list-input--active': isFocus == 3, showCloseBtn: invite != '' }">
						<input type="text" placeholder="请输入邀请码" v-model="invite" placeholder-class="inputStyle"
						@blur="blurInput(3)"
						@focus="focusInput(3)"/>
						<view class="operateBox-formBox-list-image">
							<view class="operateBox-formBox-list-image__clear operateBox-formBox-list-image__clear--active" @click="clearData('invite')">
								<image src="../../static/public/icon-clear.png" mode="widthFix"></image>
							</view>
						</view>
					</view>
				</view>
				<view class="operateBox-formBox">
					<view class="operateBox-formBox-list-header">验证码</view>
					<view class="operateBox-formBox-list operateBox-formBox-listBorder" :class="{ 'operateBox-formBox-list-input--active': isFocus == 2, showCloseBtn: code != '' }">
						<input type="number" placeholder="请输入短信验证码" v-model="code" placeholder-class="inputStyle"
						@blur="blurInput(2)"
						@focus="focusInput(2)"/>
						<view class="operateBox-formBox-list-image">
							<view class="operateBox-formBox-list-image__clear operateBox-formBox-list-image__clear--active" @click="clearData('code')">
								<image src="../../static/public/icon-clear.png" mode="widthFix"></image>
							</view>
							<view class="operateBox-formBox-list-image__code" @click="sendCode()">
								<text class="code">{{ remain }}</text>
							</view>
						</view>
					</view>
				</view>
			
				<view class="initBtn initBtn--top" @tap.stop="$u.throttle(confirm,1000)">注册</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		components:{
		},
		data(){
			return{
				name: '',
				phone: '',
				code: '',
				invite: '',
				password: '',
				re_password: '',
				pay_password: '',
				// 验证码
				remain: '获取验证码',
				isSend: true,
				time: 60,
				timer: null,
				isFocus: null,
				eyeShow: '../../static/public/icon-eye-open.png',
				eyeHide: '../../static/public/icon-eye-close.png',
				eyeShowHideArray: [{ show: false }, { show: false }, { show: false }, { show: false }, { show: false }, { show: false }, { show: false }],
				isAgree: false,
			}
		},
		onLoad() {
			
		},
		onShow() {
			let hash = document.URL.split('?')[1];
			console.log("hash：",hash);
			if(hash){
				// this.invite = this.getQueryString('invite');
				this.invite = hash.split('invite=')[1];
			}
			console.log("invite:",this.invite);
		},
		methods: {
			confirm(){
				let that = this;
				if (that.name == '' ) {
					return that.$tools.toast('请输入用户昵称');
				}
				if (that.phone == '' ) {
					return that.$tools.toast('请输入手机号');
				}
				if (that.phone.length!=11 || !/^((1[0-9])+\d{9})$/.test(that.phone)){
					return that.$tools.toast('请输入正确的手机号码！！！');
				}
				if (that.password == '' ) {
					return that.$tools.toast('请输入登录密码');
				}
				var reg = /^(?![0-7]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/;
				if(!reg.test(that.password)){
					return that.$tools.toast('请输入6-12位字母+数字的登录密码');
				}
				if (that.pay_password == '' ) {
					return that.$tools.toast('请输入交易密码');
				}
				var reg1 = /^\d{6}$/;
				if(!reg1.test(that.pay_password)){
					return that.$tools.toast('请输入6位数字交易密码');
				}
				if (that.invite == '' ) {
					return that.$tools.toast('请输入邀请码');
				}
				if (that.code == ''){
					return that.$tools.toast('请输入手机验证码');
				}
				// if (that.re_password == '' ) {
				// 	return that.$tools.toast('确认密码');
				// }
				// if (that.re_password != that.password ) {
				// 	return that.$tools.toast('两者密码不一致');
				// }
				var data = {};
				data['nickName'] = that.name;
				data['account'] = that.phone;
				data['code'] = that.code;
				data['inviteUserCode'] = that.invite ? that.invite : '';
				data['password'] = that.$md5(that.password);
				data['twicePassword'] = that.$md5(that.password);
				data['payPwd'] = that.$md5(that.pay_password);
				console.log("data：",data);
				uni.showLoading({
					mask: true
				});
				that.$Ajax('/api/user/register',data,res => {
						if (res.code == 200) {
							that.$tools.toastJump('注册成功','./download');
						}
						uni.hideLoading();
					}
				)
			},
			sendCode() {
				if (this.phone == '' ) {
					return this.$tools.toast('请输入手机号');
				}
				if (this.phone.length!=11 || !/^((1[0-9])+\d{9})$/.test(this.phone)){
					return this.$tools.toast('请输入正确的手机号码！！！');
				}
				if(this.isSend == true){
					uni.showLoading({
						title: '发送中',
						mask: true
					});
					var data = {};
					data['account'] = this.phone;
					data['type'] = 7;
					console.log("data：",data);
					this.$Ajax('/api/user/sendCode',
					data,
					res => {
							this.isSend = false;
							this.remain = this.time + 's';
							this.timer = setInterval(() => {
								this.time--;
								this.remain = this.time + 's';
								if (this.time == 0) {
									clearInterval(this.timer);
									this.remain = '重新获取';
									(this.isSend = true), (this.time = 60);
								}
							}, 1000);
						}
					)
					uni.hideLoading();
				}
			},
			checkArgee(){
				this.isAgree = !this.isAgree;
			},
			/* clear & eye */
			focusInput(index) {
				this.isFocus = index;
			},
			blurInput(index) {
				this.isFocus = null;
			},
			clearData(keyName) {
				for (var key in this.$data) {
					if (key == keyName) {
						this.$data[key] = '';
					}
				}
			},
			toggleEyeStatus(index) {
				this.eyeShowHideArray[index].show = !this.eyeShowHideArray[index].show;
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/common/scss/pages/operate/register';
	.pages{
		background: url('@/static/operate/loginBg.png');
		background-repeat: no-repeat;
		background-size: 100% 100%;
		min-height: 100vh;
		width: 100vw;
		padding-bottom: 5%;
	}
</style>